import 'package:flutter/material.dart';
import 'package:flutter_ui_demo/page/component/component.dart';
import 'package:flutter_ui_demo/page/others/others_page.dart';
import 'package:flutter_ui_demo/page/setting/setting_page.dart';
import 'package:flutter_ui_demo/page/widget/widget_page.dart';
import 'package:flutter_ui_demo/res/colors.dart';
import 'package:flutter_ui_demo/res/strings.dart';
import 'package:custom_widgets/custom_widgets.dart';

class MainPage extends StatefulWidget {
  /// 选中的tab项索引
  // final int selectedIndex;
  // MainPage({Key key, this.selectedIndex}) : super(key: key);
  MainPage({Key? key}) : super(key: key);

  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  /// 选中的tab项索引
  int selectedIndex = 0;

  PageController? pageController;

  final List<Widget> tabPages = [
    WidgetPage(),
    ComponentPage(),
    OthersPage(),
    SettingPage(),
  ];

  @override
  void initState() {
    var cal = Calculator();
    print("测试custom_widgets自定义库的使用: ${cal.addOne(2019)}");
    super.initState();
    this.selectedIndex = selectedIndex;
    this.pageController = PageController(initialPage: selectedIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody(),
      bottomNavigationBar: _bottomTab(),
    );
  }

  ///build main body.
  Widget _buildBody() {
    return Stack(
      children: <Widget>[
        PageView(
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            _scrollPageChange(context, index);
          },
          controller: pageController,
          children: tabPages,
        ),
      ],
    );
  }

  Widget _bottomTab() {
    var _bottomTabs = <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: AppStrings.TAB_WIDGET,
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.extension),
        label: AppStrings.TAB_COMPONENT,
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.perm_media),
        label: AppStrings.TAB_OTHERS,
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.settings),
        label: AppStrings.TAB_SETTING,
      )
    ];
    return Theme(
      data: ThemeData(
        brightness: Theme.of(context).brightness,
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
      ),
      child: BottomNavigationBar(
        items: _bottomTabs,
        type: BottomNavigationBarType.fixed,
        currentIndex: selectedIndex,
        selectedFontSize: 12,
        selectedItemColor: Colours.app_main,
        backgroundColor: Colors.white,
        onTap: _pageChange,
      ),
    );
  }

  /// 点击底部tab切换
  void _pageChange(int index) {
    setState(() {
      selectedIndex = index;
    });
    pageController?.jumpToPage(index);
  }

  /// 切换事件触发
  void _scrollPageChange(BuildContext context, int index) {
    setState(() {
      selectedIndex = index;
    });
  }
}
